<?php



class addComment
{
	var $submit, $catagory, $catagory_item, $messageToOptions, $messageToShow, $title, $message, $introMessage, $mask_id, $mask_name;

	//catagory the comment belongs too, the item in the catagory i.e. which incident, action to use when submiting the data, the default title in the message, the default message in the message
	//array of possible drivers to send a message too, the users id in the array, the item name in the array, 
	function addComment($catagory, $catagory_item, $submit_action, $default_title = '', $default_message = '', $addCommentTitle = '', 
						$addCommentMessage = '', $messageTo = Array(), $everyoneOption = true, $arrayID = 'user_id', $arrayName = 'pf_drivername', 
						$mask_id=0, $mask_name='' )
	{
		global $lang, $properties;
		
		include_once 'includes/addMessage.php';
		
		if(! isset($_SESSION['valid_id']))
			return;
		
		if($addCommentTitle == '')
		{
			$this->introMessage = new addMessage(sprintf($lang['CommentAddMessage'], $properties['user_comments_edit']) . '<br>' . $addCommentMessage , $lang['CommentAddTitle'], false, true);
		}
		else
		{
			$this->introMessage = new addMessage(sprintf($lang['CommentAddMessage'], $properties['user_comments_edit']). '<br>' . $addCommentMessage , $addCommentTitle, false, true);
		}
		
		$this->submit = $submit_action;
		$this->catagory = $catagory;
		$this->catagory_item = $catagory_item;
		$this->title = formatFormOutput($default_title);
		$this->message = formatFormOutput($default_message);
		$this->messageToOptions =  formatFormOutput($everyoneOption) ? '<option value="0">' . $lang['CommentAllDrivers'] . '</option>' : '';
		$this->mask_id = $mask_id;
		$this->mask_name = $mask_name;
		$this->messageToShow = false;
		if(is_array($messageTo))
		{
			foreach($messageTo as $driver)
			{
				$this->messageToShow = true;
				$this->messageToOptions .= '<option value="' . $driver[$arrayID] .  '">' . $driver[$arrayName] . '</option>';
			}
		}
	}
	
	
	function render()
	{		
		if(! isset($_SESSION['valid_id']))
			return;
	
		global $lang, $skin_dir;
	
		$submit = $this->submit;
		$catagory = $this->catagory;
		$catagoryItem = $this->catagory_item;
		$title = $this->title;
		$message = $this->message;
		$mask_id = $this->mask_id;
		$mask_user = str_replace('{USER}', $this->mask_name, $lang['CommentMask']);
		
		if($mask_id == 0)
		{
			$remMaskS = '<!--';
			$remMaskE = '-->';
		}
		else
		{
			$remMaskS = '';
			$remMaskE = '';
		}
		
		if($this->messageToShow)
		{
			$remOptionS = '';
			$remOptionE = '';
			$remHiddenS = '<!--';
			$remHiddenE = '-->';
		}		
		else
		{
			$remOptionS = '<!--';
			$remOptionE = '-->';
			$remHiddenS = '';
			$remHiddenE = '';
		}
		
		$messageOptions = $this->messageToOptions;
		
		$this->introMessage->render();
		include $skin_dir . 'tpl/addComment.tpl'; 
	}
}

//renders a comment, mode options = "ID" or "DATA"
function renderComment($mode, $data, $reRender = false)
{
	global $db, $lang, $skin_dir, $properties;
	
	if($mode == "DATA")
		$comment = $data;
	else if($mode == "ID")
	{
		$q = 'SELECT c.*, p.pf_drivername as co_username, t.pf_drivername as co_commentToUsername, 
						 date_format(co_date, "%M %e, %Y, %l:%i%p") as co_dateF ,
					 	NOW() < DATE_ADD(co_date, INTERVAL ' . $properties['user_comments_edit'] . ' MINUTE) as co_editPermission
				  FROM ' . DRIVERNAME_TABLE . ' p, ' . COMMENTS_TABLE . ' c
				  LEFT JOIN ' . DRIVERNAME_TABLE . ' t
				  ON co_commentTo = t.user_id
				  WHERE co_user = p.user_id AND co_id = ' . $data;
				  
		$res = $db->sql_query($q);
		if ( $comment = $db->sql_fetchrow($res)  )
		{
		}
		else
			return false;
	}
	else
	{
		return false;
	}
	
		
	$user = '<a href="drivers.php?id=' . $comment['co_user'] . '">' . $comment['co_username'] . '</a>';
	$title = '<b>' . $comment['co_title'] . '</b>';
	
	if($comment['co_commentTo'] == 0)
	{
		$title =  formatOutput(sprintf($lang['CommentViewTitle'], $title, $user, $comment['co_dateF']));
	}
	else
	{
		$to = '<a href="drivers.php?id=' . $comment['co_commentTo'] . '">' . $comment['co_commentToUsername'] . '</a>';
		$title =  formatOutput(sprintf($lang['CommentViewTitleTo'], $title, $user, $comment['co_dateF'], $to));
	}
	
	if($comment['co_edited'] != '')
	{
		$editedBy = formatOutput($comment['co_edited']);
		$editS = '';
		$editE = '';
	}
	else
	{
		$editedBy = '';
		$editS = '<!--';
		$editE = '-->';
	}
		
	$message = formatOutput($comment['co_text']);
	
	$id = 'comment' . $comment['co_id'];
	
	if((isset($_SESSION['valid_admin']) && $_SESSION['valid_admin'] > 1) || 
			($comment['co_editPermission'] == 1 && isset($_SESSION['valid_id']) && $_SESSION['valid_id'] == $comment['co_user']))
	{
		$adminOptions = '<a href="javascript:makeRequest(\'ajax/comments.php\', \'?id=' . $comment['co_id'] . 
							'&mode=edit\', \'' . $id . '\')">' . $lang['EditButton'] . '<a/> | ' .
							'<a href="javascript:makeRequestConfirm(\'ajax/comments.php\', \'?id=' . 
							$comment['co_id'] . '&mode=delete\', \'' . $id . '\')">' . 
							$lang['RemoveButton'] . '</a>';
	}
	else
	{
		$adminOptions = '';
	}
	
	if($reRender)
	{
		$formS = '<!--';
		$formE = '-->';
	}
	else
	{
		$formS = '';
		$formE = '';
	}
	
	include $skin_dir . 'tpl/commentView.tpl'; 
	
	return true;
}

class listComments
{
	var $introMessage, $comments, $hide = false, $comment_added;

	//catagory the comment belongs too, the item in the catagory i.e. which incident, title of the list of comments, view mode: 0 = all, 1 = to or from me only, 2 = to or from me or everyone only 
	function listComments($catagory, $catagory_item, $comment_title, $view_mode, $hide_if_none = false)
	{
                global $style;
                $style->addJavascript('js/ajax.js');
                $style->addJavascript('js/comments.js');


		global $lang, $db, $properties;
		
		include_once 'includes/addMessage.php';
		
		$message = '';
		
		if(isset($_POST['submitIncident']) && isset($_POST['commentCatagory']) && $_POST['commentCatagory'] == $catagory)
		{
			$message .= $this->addComment() . '<br><br>';
		}
		
		if($view_mode == 1)
		{
			$view_select = ' AND (co_user = ' . $_SESSION['valid_id'] . ' OR co_commentTo = ' . $_SESSION['valid_id'] . ')';
		}
		else if($view_mode == 2)
		{
			$view_select = ' AND (co_user = ' . $_SESSION['valid_id'] . ' OR co_commentTo = ' . $_SESSION['valid_id'] . ' OR co_commentTo = 0)';
		}
		else
		{
			$view_select = '';
		}
		
		$q = 'SELECT co_id, co_title, co_text, co_user, co_edited, u.pf_drivername as co_username, 
					 co_commentTo, t.pf_drivername as co_commentToUsername, date_format(co_date, "%M %e, %Y, %l:%i%p") as co_dateF,
					 NOW() < DATE_ADD(co_date, INTERVAL ' . $properties['user_comments_edit'] . ' MINUTE) as co_editPermission
			  FROM ' . DRIVERNAME_TABLE . ' u, ' . COMMENTS_TABLE . '
			  LEFT JOIN ' . DRIVERNAME_TABLE . ' t ON(co_commentTo = t.user_id)
			  WHERE co_catagory = ' . $catagory . ' AND co_catagory_item = ' . $catagory_item . ' AND u.user_id = co_user ' . $view_select . '
			  ORDER BY co_date ASC'; 
			  
		$result = $db->sql_query($q);
		$comment_count = $db->sql_numrows($result);
		if($comment_count == 0)
		{
			$this->introMessage = new addMessage( $message . $lang['CommentNoComments'], $comment_title, false, true);
			if($hide_if_none == true)
			{
				$this->hide = true;
			}
		}
		else
		{
			$this->introMessage = new addMessage( $message . str_replace('{COUNT}', $comment_count, $lang['CommentViewCount']) . 
												 ($view_mode == 1 ? '<br>' . $lang['CommentDirectOnly']  : ''), $comment_title, false, true);
			
			while ($row = $db->sql_fetchrow($result))
			{
				$this->comments[] = $row;
			}
			
		}
		
		
		
	}
	
	
	function addComment()
	{
		global $lang, $db;
	
		//prevent comment being added twice
		unset($_POST['submitIncident']);
	
		if(! isset($_POST['commentMessage']) || $_POST['commentMessage'] == '' || 
			! isset($_POST['commentMessageTo']) || ! isset($_POST['commentCatagory']) ||
			! isset($_POST['commentCatagoryItem']) || ! isset($_POST['commentTitle']) || 
			! is_numeric($_POST['commentCatagory']) || ! is_numeric($_POST['commentCatagoryItem']) || 
			! is_numeric($_POST['commentMessageTo']) )
		{
			return $lang['CommentAddFailed'];
		}
		else
		{
			//are we masking ourselves?
			if(isset($_POST['maskUser']) && is_numeric($_POST['maskUser']))
			{
				$user_id = $_POST['maskUser'];			
			}
			else
			{
				$user_id = $_SESSION['valid_id'];
			}
			
			$q = 'INSERT INTO ' . COMMENTS_TABLE . ' (co_catagory, co_catagory_item, co_user, co_date, co_text, co_commentTo, co_title) 
				  VALUES (' . $_POST['commentCatagory'] . ', ' . $_POST['commentCatagoryItem'] . ', ' . $user_id . ', NOW(), "' . 
				  		  $_POST['commentMessage'] . '", ' . $_POST['commentMessageTo'] . ', "' . $_POST['commentTitle'] . '")';
		  
			$db->sql_query($q);
			if($db->sql_affectedrows() == 1)
			{
				$this->comment_added = Array();
				$this->comment_added['catagory'] = $_POST['commentCatagory'];
				$this->comment_added['item'] = $_POST['commentCatagoryItem'];
				$this->comment_added['message'] = $_POST['commentMessage'];
				$this->comment_added['title'] = $_POST['commentTitle'];
				$this->comment_added['to'] = $_POST['commentMessageTo'];
				return $lang['CommentAdded'];
			}
			else
			{
				return $lang['CommentAddFailed'];
			}
		}
	}
	
	function commentAdded()
	{
		return $this->comment_added;
	}
	
	function commentAddedCatagory()
	{
		return is_array($this->comment_added) ? $this->comment_added['catagory'] : 0;
	}
	
	function render()
	{
		if($this->hide == false)
		{
			global $lang, $skin_dir;
				
			
			$this->introMessage->render();
			
			if(sizeof($this->comments) > 0)
			{
				#include $skin_dir . 'tpl/listStart.tpl';
				
				foreach($this->comments as $comment)
				{
					renderComment("DATA", $comment);
				}
				
				
				#include $skin_dir . 'tpl/listEnd.tpl';
				echo '<br/>';
			}
		}
	}
}


class commentStatistics
{
	var $stats = Array();

	//Passed an array of Comment topic IDs to find. Each item in an array is an array itself with values for 'catagory' and 'item'
	function commentStatistics($commentIDs)
	{
		global $lang, $db;
		
		include_once 'includes/addMessage.php';
		
		$filter = '';
		
		foreach($commentIDs as $id)
		{
			if(isset($id['catagory']) && isset($id['item']))
			{
				if($filter == '' && $id['item'] > 0)
				{
					$filter = ' (c.co_catagory = ' . $id['catagory'] . ' AND c.co_catagory_item = ' . $id['item'] . ' ) ';		
				}
				else if($filter == '' && $id['item'] <= 0)
				{
					$filter = ' (c.co_catagory = ' . $id['catagory'] . ' ) ';		
				}
				else if($filter != '' && $id['item'] > 0)
				{
					$filter .= ' OR (c.co_catagory = ' . $id['catagory'] . ' AND c.co_catagory_item = ' . $id['item'] . ' ) ';
				}
				else
				{
					$filter .= ' OR (c.co_catagory = ' . $id['catagory'] . ' ) ';
				}
			}
		}
		
		//Last post details
		$q =   'SELECT c.*, (SELECT count(*) 
							 FROM lc_comments c3 
							 WHERE c3.co_catagory = c.co_catagory AND c3.co_catagory_item = c.co_catagory_item 
							 GROUP BY c3.co_catagory, c3.co_catagory_item) as co_count
				FROM ' . COMMENTS_TABLE . ' c
				LEFT JOIN ' . COMMENTS_TABLE . ' c2
				ON c.co_catagory = c2.co_catagory AND c.co_catagory_item = c2.co_catagory_item AND c.co_date < c2.co_date
				WHERE c2.co_id IS NULL AND ' . $filter . '
				GROUP BY c.co_catagory, c.co_catagory_item'; 
							  			  
		$result = $db->sql_query($q);
		while ($row = $db->sql_fetchrow($result))
		{
			$this->stats[$row['co_catagory'] . '-' . $row['co_catagory_item']] = $row;
		}
	}
	
	
	function getAllStats()
	{
		return $this->stats;
	}
	
	function getStat($catagory, $item)
	{
		if(isset($this->stats[$catagory . '-' . $item]))
		{
			return $this->stats[$catagory . '-' . $item];
		}
		else
		{
			return NULL;
		}
	}
}

?>